package com.zheng.study.base.service.redis;

import com.zheng.study.base.utils.LogUtil;
import org.apache.commons.lang3.StringUtils;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

/**
 * Redis相关方法
 * ============================================================================
 * author : dzw
 * createDate:  2018/8/17 。
 * ============================================================================
 */
public class RedisService {

	private ShardedJedisPool shardedJedisPool;

	public ShardedJedisPool getShardedJedisPool() {
		return shardedJedisPool;
	}

	public void setShardedJedisPool(ShardedJedisPool shardedJedisPool) {
		this.shardedJedisPool = shardedJedisPool;
	}


	/**
	 * 查询redis中一个key是否存在
	 */
	public boolean exists(String key){
		ShardedJedis shardedJedis = null;
		try {
			shardedJedis = shardedJedisPool.getResource();
			return shardedJedis.exists(key);
		}catch (Exception ex){
			LogUtil.error("exists error", ex);
		}finally {
			closeShardedJedis(shardedJedis);
		}
		return false;
	}

	/**
	 * 将 key 中储存的数字值增一
	 */
	public long incr(String key){
		ShardedJedis shardedJedis = null;
		try {
			shardedJedis = shardedJedisPool.getResource();
			return shardedJedis.incr(key);
		} catch (Exception e){
			LogUtil.error("incr error", e);
		}finally {
			closeShardedJedis(shardedJedis);
		}
		return 0;
	}

	/**
	 * 将值 value 关联到 key
	 */
	public boolean set(String key, String value) {
		ShardedJedis shardedJedis = null;
		try {
			shardedJedis = shardedJedisPool.getResource();
			shardedJedis.set(key, value);
			return true;
		}catch (Exception e){
			LogUtil.error("set error", e);
		}finally {
			closeShardedJedis(shardedJedis);
		}
		return false;
	}

	/**
	 * 将值 value 关联到 key ，并将 key 的生存时间设为 seconds (以秒为单位)
	 */
	public boolean set(String key, String value, int second){
		ShardedJedis shardedJedis = null;
		try {
			shardedJedis = shardedJedisPool.getResource();
			shardedJedis.setex(key, second, value);
			return true;
		}catch (Exception e){
			LogUtil.error("setex error", e);
		}finally {
			closeShardedJedis(shardedJedis);
		}
		return false;
	}

	/**
	 * 通过key获得value
	 */
	public String get(String key){
		ShardedJedis shardedJedis = null;
		try {
			shardedJedis = shardedJedisPool.getResource();
			return shardedJedis.get(key);
		} catch (Exception e){
			LogUtil.error("get error", e);
		} finally {
			closeShardedJedis(shardedJedis);
		}
		return null;
	}

	/**
	 * 设置一个key的过期时间
	 * @return 1：设置了过期时间  0：没有设置过期时间/不能设置过期时间
	 */
	public long expire(String key, int seconds){
		if(StringUtils.isNotEmpty(key)){
			ShardedJedis shardedJedis = null;
			try {
				shardedJedis = shardedJedisPool.getResource();
				return shardedJedis.expire(key, seconds);
			} catch (Exception e){
				LogUtil.error("EXPIRE error[key=" + key + " seconds=" + seconds + "]" + e.getMessage(), e);
			} finally {
				closeShardedJedis(shardedJedis);
			}
		}
		return 0;
	}


	private void closeShardedJedis(ShardedJedis shardedJedis) {
		try {
			if(shardedJedis!=null){
				shardedJedis.close();
			}
		}catch (Exception e){
			LogUtil.error("shardedJedis close error", e);
		}
	}

}
